<?php
if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
$bettype = 12;
$ui = new UI();
$tpl = new Template2();
$tpl->load(array("main" => "proc11.htm"));
$tpl->set("account", $curruser['account']);
if ($curruser['enabled'] == 0 || $curruser['enabled'] == 2)
{
    $ui->showErrorPage("<p class=error>帐号已被暂停使用</p>");
    exit();
}
$rs = getdb("select * from lotto where open>='" . date("Y-m-d") . "' order by open");
if ($rs->eof)
{
    $ui->showErrorPage("<p class=error>目前没有可以下注期数！</p>");
    exit();
}
$open = $rs->f['open'];
$round = $rs->f['round'];
$lotto_id = $rs->f['id'];
$lotto_ns = $rs->f['n1'] + $rs->f['n2'] + $rs->f['n3'] + $rs->f['n4'] + $rs->f['n5'] + $rs->f['n6'];
$conf = new Config();
$stopbet = $conf->get("waitbet");
if (date("Y-m-d") < $open)
{
    exit();
}
else if (date("Y-m-d") == $open)
{
    if ($stopbet <= date("Hi") || 0 < $lotto_ns)
    {
        $ui->showErrorPage("<p>下注期数：" . d3($round) . "<br>开奖日：" . $open . "</p>" . "<p class=error>已经截止下注！</p>");
        exit();
    }
    else if (date("Hi") <= $conf->get("startbet"))
    {
        exit();
    }
}
$tpl->set("open", $open);
$tpl->set("round", d3($round));
$tpl->set("lotto_id", $lotto_id);
$tpl->set("account", $curruser['account']);
$tpl->set("amount", $curruser['amount']);
$rs1 = getdb("select amount_6h from k_user_bet where user_id=" . $curruser['id'] . " and lotto_id=" . $lotto_id);
$usedamount = $rs1->f[0] ? $rs1->f[0] : 0;
$rs1->close();
$tpl->set("usedamount", $usedamount);
$rs1 = getdb("!!select sum(amount) from bet where bettype={$bettype} and user_id=" . $curruser['id'] . " and lotto_id=" . $lotto_id . " and rame=0");
$betamount = $rs1->f[0] ? $rs1->f[0] : 0;
$tpl->set("betamount", $betamount);
$rs1->close();
if (count($_POST) < 2)
{
    err("至少选择二组！");
}
$bets = array();
$betforms = "";
foreach ($_POST as $p => $v)
{
    if (substr($v, 0, 1) == "r")
    {
        $vv = str_replace("r", "", $v);
        $bets[] = explode("_", $vv);
        $betforms .= "<input type=hidden name=\"" . $p . "\" value=\"" . $v . "\">";
    }
}
$tpl->set("betforms", $betforms);
$idisk = getUserValidDisk($curruser);
$disk = $g_disk_code[$idisk];
echo "<input type=hidden name=disk value=" . strpos(" abcd", $disk) . ">";
$tpl->set("diskStr", strtoupper($disk) . "盘");
$rs = getdb("select * from rate16" . $disk . " where lotto_id={$lotto_id}");
if ($rs->eof)
{
    err("系统目前维护中，请稍後！");
}
$ratechange = FALSE;
$bignum = array("", "一", "二", "三", "四", "五", "六");
$thistype = array("", "单", "双", "大", "小", "红", "蓝", "绿");
$chknum = array(0, 0, 0, 0, 0, 0, 0);
$betcontent = "";
$rates = "";
$err = 0;
$win = 1;
foreach ($bets as $b)
{
    if (5 <= $b[1] && $b[1] <= 7)
    {
        $ps_rate = trimfloat($rs->f["r15_" . $b[0] . "_" . $b[1]] - 0.05, 1);
    }
    else
    {
        $ps_rate = trimfloat($rs->f["r15_" . $b[0] . "_" . $b[1]], 1);
    }
    if ($ps_rate != $b[2])
    {
        $ratechange = TRUE;
    }
    $rates .= $ps_rate . ",";
    $betcontent .= "<font color=red>正码" . $bignum[$b[0]] . ":" . $thistype[$b[1]] . "</font> @ <font color=red><b>" . $ps_rate . "</b></font><br>";
    $win *= $ps_rate;
    ++$chknum[$b[0]];
    if (2 < $chknum[$b[0]])
    {
        ++$err;
    }
}
if (0 < $err)
{
    err("每个号码最多选择二种玩法！");
}
$tpl->set("betcontent", $betcontent);
$rates = substr($rates, 0, 0 - 1);
$tpl->set("rates", $rates);
$betmin = $conf->get("betmin");
$tpl->set("min", $betmin);
$betmax = $conf->get("betmax");
$tpl->set("max", $betmax);
$tpl->set("max_str", $betmax);
$betname = "正码过关";
$tpl->set("betname", $betname);
$rs1 = getdb("select * from amounts where bettype={$bettype} and user_id=" . $curruser['id']);
$amount1 = $rs1->f['amount1'] - 0;
$amount2 = $rs1->f['amount2'] - 0;
$tpl->set("amount1", $amount1);
$tpl->set("amount2", $amount2);
$tpl->set("amount1_str", $amount1);
$tpl->set("amount2_str", $amount2);
$usemaxamount = $curruser['usemaxamount'];

if (isset($_POST['money']) && $ratechange == FALSE)
{
    $money = intval($_POST['money']);
    if ($money == 0)
    {
        write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, "使用外挂攻击");
        err("不要随便攻击本站！你的 IP 已经被记录了！");
    }
    if ($money < $betmin)
    {
        err("下注金额不能小於最低限额！");
    }
    if ($amount1 < $money)
    {
        err("下注金额不能超过单注限额！");
    }
    if ($amount2 < $money + $betamount)
    {
        err("下注金额不能超过单项限额！");
    }
    if ($curruser['balance'] < $money + $usedamount)
    {
        err("下注金额不能超过信用额度！");
    }
    if ($usemaxamount && $curruser['maxamount'] < $money + $usedamount)
    {
        err("下注金额不能超过本期限额！");
    }
    if (500000 < $money * $win - $money)
    {
        err("预估可赢金额超过最大理赔限额 500,000");
    }
    if ($conf->get("waitbet") <= date("Hi"))
    {
        err("已经截止下注！");
    }
    $payoff = $money;
    $fields = array();
    $fields['user_id'] = $curruser['id'];
    $fields['lotto_id'] = $lotto_id;
    $fields['createtime'] = systime();
    $fields['bettype'] = $bettype;
    $nnn1 = "";
    $nnn2 = "";
    $errs = 0;
    $numbers = "";
    foreach ($bets as $p)
    {
        $nnn1 = implode("_", array_values($p));
        if (substr($nnn1, 1, 1) != "_" || substr($nnn1, 3, 1) != "_")
        {
            ++$errs;
        }
        if (substr($nnn1, 0, 4) == substr($nnn2, 0, 4))
        {
            ++$errs;
        }
        $numbers .= $nnn1 . ",";
        $payoff = $payoff * $p[2];
        $nnn2 = $nnn1;
    }
    if (0 < $errs || $numbers == "")
    {
        write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, "使用不正常方式下注（" . $numbers . "）");
        account_err("请勿以非正常方式下注！你的 IP 已经被记录并且也通报你的上层管理员了！");
    }
    $payoff = round($payoff - $money);
    $numbers = substr($numbers, 0, 0 - 1);
    $fields['numbers'] = "'{$numbers}'";
    $fields['amount'] = $money;
    $fields['rate'] = 0;
    $fields['payoff'] = $payoff;
    $fields['disk'] = $idisk;
    $fields['line_nums'] = "'{$numbers}'";
    $fields['unitmoney'] = $money;
    $u2 = get_user($curruser['prev_id']);
    $u3 = get_user($u2['prev_id']);
    $u4 = get_user($u3['prev_id']);
    $u5 = get_user($u4['prev_id']);
    $fields['acl2'] = $u2['id'];
    $fields['acl3'] = $u3['id'];
    $fields['acl4'] = $u4['id'];
    $fields['acl5'] = $u5['id'];
    $a1 = GetBack($curruser['id'], $fields['bettype'], $idisk, 1);
    $a2 = GetBack($u2['id'], $fields['bettype'], $idisk, 2);
    $a3 = GetBack($u3['id'], $fields['bettype'], $idisk, 3);
    $a4 = GetBack($u4['id'], $fields['bettype'], $idisk, 4);
    $a5 = GetBack($u5['id'], $fields['bettype'], $idisk, 5);
    $a6 = GetBack($u5['supe_account'], $fields['bettype'], $idisk, 255);
    $fields['back1'] = $a1;
    $fields['back2'] = $a2;
    $fields['back3'] = $a3;
    $fields['back4'] = $a4;
    $fields['back5'] = $a5;
    $fields['back6'] = $a6;
    $protmod = $u5['protmod'];
    $fields['mpercent'] = $u2['mpercent'];
    $fields['percent2'] = $u2['percent0'];
    $fields['percent3'] = min($u3['percent1'], $u4['mpercent'] - $fields['mpercent'] - $fields['percent2']);
    $fields['percent4'] = min($u4['percent1'], $u5['mpercent'] - $fields['mpercent'] - $fields['percent2'] - $fields['percent3']);
    $fields['upercent'] = 100 - $fields['mpercent'] - $fields['percent2'] - $fields['percent3'] - $fields['percent4'];
    if ($fields['percent3'] < 0 || $fields['percent4'] < 0 || $fields['upercent'] < 0)
    {
        err("占成参数设置错误，注单无法存档！");
        return;
    }
    $fields['r2'] = $money * $fields['percent2'] / 100;
    $fields['r3'] = $money * $fields['percent3'] / 100;
    $fields['r4'] = $money * $fields['percent4'] / 100;
    $fields['r6'] = $money * $fields['upercent'] / 100;
    $fields['ip'] = "'" . getIP() . "'";
    $newid = db_insert("bet", $fields);
    if (!$newid)
    {
        err("超过信用额度");
    }
	$rs=getdb("select user_id from k_user_bet where user_id='".$curruser['id']."' and lotto_id='$lotto_id'");
	if(!$rs->eof)
		getdb("update k_user_bet set amount_6h=amount_6h+".$money." where user_id='".$curruser['id']."' and lotto_id='$lotto_id'");
	else
		getdb("insert into k_user_bet set amount_6h=amount_6h+".$money." ,user_id='".$curruser['id']."',lotto_id='$lotto_id'");
    $tpl->load("succ", "succ.htm");
    $tpl->set("ordertype", $betcontent);
    $tpl->set("money", $money);
    $tpl->set("payoff", $payoff);
    $tpl->set("myscript", "parent.mainFrame.location.reload();");
    $tpl->show("succ");
}
else
{
    if ($ratechange == TRUE)
    {
        if (isset($_POST['money']))
        {
            $money = intval($_POST['money']);
            $tpl->set("money", $money);
        }
        $tpl->set("message", "<tr><td colspan=2 bgcolor=red align=center><font color=white>赔率已经变动！</font></td></tr>");
    }
    $tpl->show("main");
}